package com.kstech.mp3604.fragments

import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.Button
import androidx.navigation.fragment.findNavController
import com.blankii.androidlibrary.base.BaseBindingFragment
import com.blankj.utilcode.util.SPUtils
import com.kstech.mp3604.base.Global
import com.kstech.mp3604.databinding.FragmentSetParamBinding
import com.kstech.mp3604.manager.serialComm.SerialUiListener

/**
 * 设置-参数设置
 * 相当于初始化参数配置界面
 */
class SetParamFragment :
    BaseBindingFragment<FragmentSetParamBinding>(FragmentSetParamBinding::inflate),
    SerialUiListener {
    override fun onCreateInit() {

    }

    private var factoryModel = 0
    private var dkModel = 0 //0默认未配置 1左洞口 2右洞口

    override fun onInitView() {
        val whsi = SPUtils.getInstance().getInt(Global.WARE_HOUSE_SETTING_ID)
        binding.spinnerDk.setSelection(whsi)

        binding.btnBack.setOnClickListener {
            findNavController().navigateUp()
        }
        binding.btn1.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2000")
        }
        binding.btn2.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2001")
        }
        binding.btn3.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2002")
        }
        binding.btn4.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2003")
        }
        binding.btn5.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2004")
        }
        binding.btn6.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2005")
        }
        binding.btn7.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2006")
        }
        binding.btn8.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2007")
        }
        binding.btn9.setOnClickListener {
            showConfirmDialog(this, (it as Button).text.toString(), "2008")
        }
        binding.btn10.setOnClickListener {
            showConfirmDialog(this, "出厂测试模式", "2009", factoryModel)
        }
        binding.btn11.setOnClickListener {
            showConfirmDialog(this, "洞库入口位置", "2105", dkModel)
        }

//        val spinnerAdapter = ArrayAdapter.createFromResource(
//            requireContext(), R.array.spinner_items, android.R.layout.simple_spinner_item
//        )
//        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item)
//        binding.spinner.adapter = spinnerAdapter
        binding.spinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                // 当用户选择了某一项时调用
//                val selectedItem = p0!!.getItemAtPosition(p2).toString()
                factoryModel = p2
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
                // 当没有选择任何项时调用
            }
        }

        binding.spinnerDk.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
                dkModel = p2
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
                // 当没有选择任何项时调用
            }
        }


    }

    /**
     * 弹出确认界面防止误触
     * factory 只有address是2009时需要填写
     */
    private fun showConfirmDialog(
        listener: SerialUiListener, text: String, address: String, value: Int = 0
    ) {
        DialogConfirmFragment().bindData(listener, text, address, value)
            .show(parentFragmentManager)
    }

    override fun finishedBiaoDing() {
        "发送完成".showSnackBar()
    }

    override fun saveWareHouseSettingId() {
        "保存洞库入口位置成功".showSnackBar()
    }

}